package designMode.iteratorPattern;

/**
 * @author LIBOFEI
 * @ClassName DinnerMenuIterator
 * @description
 * @date 2020/6/26 16:31
 * @Version 1.0
 **/
public class DinnerHouseMenuIterator implements java.util.Iterator<MenuItem>{

    MenuItem[] items;
    int position = 0;

    public DinnerHouseMenuIterator(MenuItem[] menuItems){
        this.items = menuItems;
    }

    @Override
    public boolean hasNext() {
        if(position >= items.length || items[position] == null){
            return false;
        }
        return true;
    }

    @Override
    public MenuItem next() {
        MenuItem menuItem = items[position];
        position++;
        return menuItem;
    }

    @Override
    public void remove() {
        if(position <= 0){
            throw new IllegalStateException("You Can't remove index is zero");
        }
        if(items[position-1] != null){
            for(int i = position - 1 ; i < (items.length - 1 ) ; i++){
                items[i] = items[i + 1];
            }
            items[items.length-1] = null;
        }

    }
}
